﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SuperQuiz.Model;
using Windows.ApplicationModel;
using Windows.Data.Xml.Dom;
using Windows.UI;
using Windows.UI.Xaml;

namespace SuperQuiz.QualityChecklist
{
    public class QualityChecklistDataSource
    {
        public async static Task<ObservableCollection<QuizCategory>> LoadQualityChecklistQuiz()
        {
            var xmlFile = await Package.Current.InstalledLocation.GetFileAsync(@"QualityChecklist\QualityChecklist.xml");
            var settings = new XmlLoadSettings() { ProhibitDtd = false, ResolveExternals = false };
            var xmlDocument = await XmlDocument.LoadFromFileAsync(xmlFile, settings);
            var xmlNamespace = "xmlns:my='http://schemas.microsoft.com/office/infopath/2003/myXSD/2012-06-18T03:50:28'";
            return await Task.Run(() =>
            {
                var answerTemplates = new List<QuizAnswer>() { 
                    new QuizAnswer()
                                {
                                    Name = "Sim",
                                    //Description = "",
                                    //ImageUri = "SuperQuiz/Assets/Images/geography.png",
                                    Points = 2,
                                },
                    new QuizAnswer()
                                {
                                    Name = "Não",
                                    //Description = "",
                                    //ImageUri = "SuperQuiz/Assets/Images/geography.png",
                                    Points = -2,
                                },
                    new QuizAnswer()
                                {
                                    Name = "Não sei",
                                    //Description = "",
                                    //ImageUri = "SuperQuiz/Assets/Images/geography.png",
                                    Points = -1,
                                },
                    new QuizAnswer()
                                {
                                    Name = "Não se aplica",
                                    //Description = "",
                                    //ImageUri = "SuperQuiz/Assets/Images/geography.png",
                                    Points = 0,
                                },
                
                };

                var quizCategories = new ObservableCollection<QuizCategory>();
                {
                    var category = new QuizCategory()
                    {
                        UniqueId = "AELChecklist",
                        Name = "Aplicativos Estilo Metro",
                        //ImageUri = "SuperQuiz/Assets/Images/geography.png",
                        Quizzes = new ObservableCollection<Quiz>()
                    };
                    var quiz = new Quiz()
                    {
                        UniqueId = "AELChecklist",
                        Name = "Checklist de Qualidade",
                        Description = @"Este checklist de qualidade foi baseado no checklist presente em win8.msregistration.com e serve para você fazer uma auto-avaliação do seu aplicativo.",
                        ImageUri = "QualityChecklist/AplicativoDeQualidade.png",
                        Questions = new ObservableCollection<QuizQuestion>(),
                        Category = category,
                        AllowPreviousNavigation = true,
                        EvaluateAnswerOnNext = false,
                        LockQuestionWhenEvaluated = false,
                        LockQuestionWhenNavigateToNext = false,
                        LockQuestionWhenNavigateToPrevious = false,
                        EvaluateAnswersOnFinish = false,
                        AllowShareQuestions = true,
                        ShowScoreWhilePlays = false,
                        ShowHomeButtonOnFinish = false,
                        ResultsControlFullName = typeof(SuperQuiz.QualityChecklist.QuizResultsControl).FullName,
                    };
                    category.Quizzes.Add(quiz);

                    int iQuestion = 0;
                    foreach (var group in xmlDocument.SelectNodesNS("/my:QualityChecklist/my:Group", xmlNamespace))
                    {
                        var groupName = group.SelectSingleNodeNS("my:Name", xmlNamespace).InnerText;
                        foreach (var item in group.SelectNodesNS("my:Items/my:Item", xmlNamespace))
                        {
                            iQuestion++;
                            var question = new QuizQuestion()
                            {
                                Code = String.Format("{0}_{1}", quiz.UniqueId, iQuestion),
                                Title = groupName,
                                SubTitle = item.SelectSingleNodeNS("my:ItemTitle", xmlNamespace).InnerText,
                                Description = item.SelectSingleNodeNS("my:ItemDescription", xmlNamespace).InnerText,
                                //ImageUri = "SuperQuiz/Assets/Images/geography.png",
                                Links = new ObservableCollection<QuizUriLink>(),
                                Answers = new ObservableCollection<QuizAnswer>(),
                            };
                            foreach (var link in item.SelectNodesNS("my:Links/my:Link", xmlNamespace))
                            {
                                question.Links.Add(new QuizUriLink()
                                        {
                                            Url = link.SelectSingleNodeNS("my:LinkUrl", xmlNamespace).InnerText,
                                            Title = link.SelectSingleNodeNS("my:LinkTitle", xmlNamespace).InnerText,
                                        }
                                    );
                            }
                            var iAnswer = 0;
                            foreach (var answerTemplate in answerTemplates)
                            {
                                var answer = new QuizAnswer()
                                {
                                    Code = String.Format("{0}_{1}", question.Code, iAnswer++),
                                    Name = answerTemplate.Name,
                                    Description = answerTemplate.Description,
                                    ImageUri = answerTemplate.ImageUri,
                                    Points = answerTemplate.Points,
                                    Links = answerTemplate.Links,
                                };
                                question.Answers.Add(answer);
                            }
                            quiz.Questions.Add(question);
                        }
                    }
                    quizCategories.Add(category);
                }

                return quizCategories;
            }
            );
        }
    }
}
